package com.luozongyao.examiner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luozongyao.commons.MD5;
import com.luozongyao.commons.R;
import com.luozongyao.examiner.entity.Examiner;
import com.luozongyao.examiner.entity.vo.ExaminerVo;
import com.luozongyao.examiner.service.ExaminerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luozongyao
 * @since 2021-10-25
 */
@Api(description="审查员信息管理")
@RestController
@RequestMapping("/examiner/examiner")
@CrossOrigin
public class ExaminerController {

    @Autowired
    private ExaminerService examinerService;
    //查询审查员表中所有数据
    @ApiOperation(value = "查询所有审查员的方法")
    @GetMapping("findAll")
    public R findAll() {
        List<Examiner> examinerList = examinerService.list(null);
        return R.ok().data("examinerList",examinerList);
    }
    //添加审查员接口的方法
    @ApiOperation(value = "添加审查员的方法")
    @PostMapping("addExaminer")
    public R addTeacher(@RequestBody Examiner examiner){
        QueryWrapper<Examiner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("examiner_email",examiner.getExaminerEmail());
        int count = examinerService.count(queryWrapper);
        if (count > 0){
            return R.error().message("邮箱已被注册");
        }
        String uuid = UUID.randomUUID().toString().replace("-","");
        examiner.setSalt(uuid);
        examiner.setExaminerPwd(MD5.encrypt(examiner.getExaminerPwd()+uuid));
        boolean save = examinerService.save(examiner);
        if (save==true){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //逻辑删除审查员的方法
    //逻辑删除 更新不了数据了 需要到数据库修改
    @ApiOperation(value = "逻辑删除审查员的方法")
    @DeleteMapping("{id}")
    public R removeExaminer(@ApiParam(name = "id",value = "审查员ID",required = true) @PathVariable int id) {
        boolean flag = examinerService.removeById(id);
        if (flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @PostMapping("updateExaminer")
    @ApiOperation(value = "更新修改审查员的方法")
    public R updateExaminer(@RequestBody Examiner examiner){
        boolean flag = examinerService.updateById(examiner);
        System.out.println(examiner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //重置密码
    @PostMapping("resetPassword")
    @ApiOperation(value = "重置密码")
    public R resetPassword(@RequestBody Examiner examiner){
        String uuid = UUID.randomUUID().toString().replace("-","");
        examiner.setSalt(uuid);
        examiner.setExaminerPwd(MD5.encrypt(MD5.encrypt("123456")+uuid));
        boolean flag = examinerService.updateById(examiner);
        System.out.println(examiner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //使用requestBody get请求不到 必须使用post请求
    //将json数据传递到对应对象里 required = false 表示后面数据可以没有
    //条件查询带分页
    @ApiOperation(value="条件查询带分页 获取表总记录数和分页数")
    @PostMapping("pageExaminerCondition/{current}/{limit}")
    public R pageExaminerCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) ExaminerVo examinerVo) {
        Page<Examiner> page = new Page<>(current,limit);
//        page.setSize(Long.MAX_VALUE);
        //构建审查员条件
        QueryWrapper<Examiner> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //判断条件是否为空 如果不为空拼接条件
        Integer examinerId = examinerVo.getExaminerId();
        String examinerEmail = examinerVo.getExaminerEmail();
        String begin = examinerVo.getBegin();
        String end = examinerVo.getEnd();
        if (!StringUtils.isEmpty(examinerId)){
            wrapper.like("examiner_id",examinerId);
        }
        if (!StringUtils.isEmpty(examinerEmail)){
            wrapper.eq("examiner_email",examinerEmail);
        }
        //begin end 用于时间范围查找
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("create_time",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("create_time",end);
        }
//        wrapper.orderByDesc("create_time");
        examinerService.page(page, wrapper);

        long total = page.getTotal();
        List<Examiner> records = page.getRecords();
//        System.out.println(total+"-------"+records);
        return R.ok().data("total",total).data("records",records);

    }




}

